package com.dl4jlpr;

import org.apache.commons.io.FilenameUtils;
import org.datavec.api.split.InputSplit;
import org.datavec.image.loader.BaseImageLoader;
import org.datavec.image.loader.NativeImageLoader;
import org.nd4j.common.primitives.Pair;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class LprIterable {
    private BaseImageLoader imageLoader;
    public LprIterable(int height, int width, int channels )
    {
        imageLoader=new NativeImageLoader(height, width, channels);
    }
    public Pair<INDArray, INDArray> CreatPair(URI imgpath) throws Exception
    {
        char[] c= FilenameUtils.getBaseName(imgpath.getPath()).toCharArray();
        float[] l=new float[c.length];
        for(int i=0;i<c.length;i++)
        {
            l[i]=PlateLableGenerator.CHARS.indexOf(c[i]);
        }
        INDArray loade= Nd4j.create(l,new int[]{1,c.length});
        //INDArray loade= imageLoader.asMatrix(new File(imgpath.getPath()));
        INDArray feature=imageLoader.asMatrix(new File(imgpath.getPath())).add(-127.5).mul(0.0078125);
        return new Pair<>(feature,loade);
    }
    public Iterable<Pair<INDArray, INDArray>> CreatIterable(InputSplit Data) throws Exception
    {
        List<Pair<INDArray, INDArray>> list=new ArrayList<>();
        for(int i=0;i<Data.length();i++) list.add(CreatPair(Data.locationsIterator().next()));
        return list;
    }

}
